import re, json, jsonlines

from typing import List, Dict, Optional, Union, Any

def read_jsonl(path):
    ret = []
    with  jsonlines.open(path, 'r') as f:
        for line in f.iter():
            ret.append(line)
    return ret 

def save_jsonl(path: str, dic: List[Dict[Any, Any]], mode: str='a'):
    with open(path, mode, encoding='utf-8') as f: 
        for data in dic:
            f.write(json.dumps(data,ensure_ascii=False))
            f.write('\n') 

def read_text(path: str, split_line: bool=True):
    print("split to line?", split_line)
    with open(path, 'r', encoding='utf-8') as f:
        text = f.read()
    return re.split(r'\n', text) if split_line else text

def read_json(path: str) -> Dict:
    with open(path, 'r', encoding='utf-8') as f:
        ret = json.load(f)
    return ret


def save_json(path, dic):
    with open(path,"w", encoding='utf-8') as f: 
        f.write(json.dumps(dic,ensure_ascii=False, indent=2)) 

def save_text(path, docs:Union[str,List[str]], mode='a+'):
    if isinstance(docs, str):
        docs = [docs,]


    with open(path, mode, encoding='utf-8') as f:
        for doc in docs:
            f.write(doc)
            f.write('\n')


def remove_invisible_characters(text):
    # 正则表达式匹配不可见字符，包括控制字符和格式字符
    invisible_chars_pattern = re.compile(r'[\x00-\x1F\x7F-\x9F\u200E\u200F\u2028\u2029\uFEFF]')
    # 替换不可见字符为空字符串
    cleaned_text = re.sub(invisible_chars_pattern, '', text)
    return cleaned_text


